package cate.game.activity.destinygate.po;

import cate.common.table.activity.destinygate.row.DestinyGateGiftConditionRow;
import cate.common.table.activity.destinygate.row.DestinyGateGiftRow;
import cate.common.table.activity.destinygate.row.DestinyGatePassRewardRow;
import cate.common.table.activity.destinygate.row.DestinyGateStarRewardRow;
import cate.common.table.d.GDOperation;
import cate.common.util.GameResult;
import cate.game.activity.destinygate.DestinyGateActivity;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass(value = "命运之扉单个英雄所有关卡数据")
public class DestinyGateChapter{
    @NoteField(value = "英雄ID")
    public int heroId;

    @NoteField(value = "章节获得星星数")
    public int star;

    @NoteField(value = "章节获得成就数")
    public int achievement;

    @NoteField(value = "本章节的关卡信息", detail = "map<关卡id，关卡信息>")
    public Map<Integer, DestinyGateLevel> levelMap;

    @NoteField("星星数奖励领取记录")
    public List<Integer> starRewardList;

    @NoteField("是否完美通关")
    public boolean pass;

    @NoteField("是否领取完美挑战奖励")
    public boolean fullPassGot;

    @JsonIgnore
    @NoteField("礼包是否全部购买")
    public boolean allBuy;

    @NoteField("礼包")
    public Map<Integer, DestinyGateGift> giftMap;


    public void initChapter(int heroId) {
        this.heroId = heroId;
        if (levelMap == null) {
            levelMap = new HashMap<>();
        }
        levelMap.values().forEach(DestinyGateLevel::initLevel);
        if (starRewardList == null) {
            starRewardList = new ArrayList<>();
        }
        if (giftMap == null) {
            giftMap = new HashMap<>();
        }
    }

    public void updateChapter() {
        star = 0;
        achievement = 0;
        pass = false;
        levelMap.values().forEach(
                a -> {
                    star += a.challenge.size();
                    achievement += a.achieve.size();
                });
        if (achievement == 20) {
            pass = true;
        }
    }

    public DestinyGateLevel showLevel(int combatNum) {
        DestinyGateLevel level = levelMap.get(combatNum);
        if (level == null) {
            level = new DestinyGateLevel(combatNum);
            level.initLevel();
        }
        levelMap.put(combatNum, level);
        return level;
    }

    //完美通关奖励
    public GameResult<DestinyGateActivity> provideChapterReward(DestinyGatePassRewardRow row, Role role) {
        GameResult<DestinyGateActivity> r = new GameResult<>();
        if (fullPassGot){
            return r.fail("该完美通关奖励已领取");
        }
        EcResult<?> exe = new MixResAdder().setRes(row.gift)
                .setOperation(GDOperation.DESTINY_GATE)
                .setDialogReward(true)
                .setSendMailWhenFull(true)
                .exe(role);
        fullPassGot = true;
        return r;
    }

    //星级奖励
    public GameResult<DestinyGateActivity> provideStarReward(int tid, DestinyGateStarRewardRow row, Role role) {
        GameResult<DestinyGateActivity> r = new GameResult<>();
        if (star < row.star){
            return r.fail("暂未达到该星级奖励领取条件");
        }
        if (starRewardList.contains(tid)){
            return r.fail("该奖励已领取");
        }
        EcResult<?> exe = new MixResAdder().setRes(row.gift)
                .setOperation(GDOperation.DESTINY_GATE)
                .setDialogReward(true)
                .setSendMailWhenFull(true)
                .exe(role);
        starRewardList.add(tid);
        return r;
    }

    /**
     * 检测是否达到礼包解锁条件
     * @return
     */
    public GameResult<Void> checkCondition(int limit, int buyTimes, List<DestinyGateGiftConditionRow> conditionRows) {
        GameResult<Void> r = new GameResult<>();
        if (conditionRows.isEmpty()) {
            return r.success();
        }
        int times = limit;
        //根据解锁条件增加次数
        for (DestinyGateGiftConditionRow conditionRow : conditionRows) {
            String condition = conditionRow.condition;
            List<Integer> conditions = new ArrayList<>();
            if (StringUtils.isNotBlank(condition)) {
                String[] split = condition.split(":");
                for (String str : split) {
                    conditions.add(Integer.parseInt(str));
                }
            }
            if (conditions.size() > 0) {
                if (conditions.get(0) == 1) {
                    //通关
                    if(levelMap.size() >= conditions.get(1)){
                        times += conditionRow.addTime;
                    }
                } else if (conditions.get(0) == 2) {
                    //星级
                    if(star >= conditions.get(1)){
                        times += conditionRow.addTime;
                    }
                } else if (conditions.get(0) == 3) {
                    //成就
                    if(achievement >= conditions.get(1)){
                        times += conditionRow.addTime;
                    }
                }
            }
        }
        if (times <= buyTimes) {
            return r.fail("该礼包已达到购买次数上限");
        }
        return r.success();
    }
}
